package bairui.international.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bairui.international.model.domain.jpa.TB_Address;
import bairui.international.model.domain.jpa.TB_City;
import bairui.international.model.domain.jpa.TB_Province;
import bairui.international.model.domain.jpa.TB_Town;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_Address;
import bairui.international.service.Service_Town;
import bairui.international.service.Service_User;

/**
 * 
 * 【(地址)TB_Address】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_Address 模型
 * @see Service_Address 服务接口
 * @see BService_AddressImpl 服务实现基础
 *
 */
@Service
public class Service_AddressImpl extends BService_AddressImpl implements Service_Address {

	@Autowired private Service_Town service_Town;
	@Autowired private Service_User service_user;
	
	@Override
	public TB_Address createInstance(
		TB_User user, String name, String phone, Long townId, String area, String detailAdddress, Boolean defaultFlag
	){
		return dao.save(new TB_Address(
				new TB_Address.MustProperties() {
					@Override public TB_User getUser() {
						return user;
					}
					@Override public Long getTownId() {
						return townId;
					}
					@Override public String getDetailAddress() {
						return detailAdddress;
					}
					@Override public Boolean getDefaultFlag() {
						return defaultFlag;
					}
					@Override public String getArea() {
						return area;
					}
					@Override public String getPhone() {
						return phone;
					}
					@Override public String getName() {
						return name;
					}
				},
				new TB_Address.OptionalSingleProperties() {

				}
			));
	}
	
	@Override
	public TB_Address createInstance(
		TB_User user, String name, String phone, TB_Town town, String detailAdddress, Boolean defaultFlag
	){
		town = service_Town.load(town);
		TB_City city = town.getCity();
		TB_Province province = city.getProvince();
		String area = province.getName() + "/" + city.getName() + "/" + town.getName();
		return createInstance(user, name, phone, town.getId(), area, detailAdddress, defaultFlag);
	}
	
	@Override
	public TB_Address add(
		TB_User user, String name, String phone, TB_Town town, String detailAdddress, Boolean defaultFlag
	){
		if(existsDefult(user)){
			TB_Address bean = createInstance(user, name, phone, town, detailAdddress, false);
			service_user
				.load(user)
				.addAddresss(bean);
			if(defaultFlag){
				setDefault(bean);
			}
			return bean;
		}
		return createInstance(user, name, phone, town, detailAdddress, true);
	}
	
	@Override
	public TB_Address edit(
		Long id, TB_User user, String name, String phone, TB_Town _town, String detailAdddress, Boolean defaultFlag
	) throws Exception{
		TB_Address bean = findOne(it->it.id.eq(id).and(it.user.eq(user)));
		TB_Town town = service_Town.load(_town);
		//
		bean.new UpdateProperties() {
			@Override public String getName(TB_Address it) throws Exception {
				return name;
			}
			@Override public String getPhone(TB_Address it) throws Exception {
				return phone;
			}
			@Override public String getDetailAddress(TB_Address it) throws Exception {
				return detailAdddress;
			}
			@Override public Long getTownId(TB_Address it) throws Exception {
				return town.getId();
			}
			@Override public Boolean getDefaultFlag(TB_Address it) throws Exception {
				return defaultFlag;
			}
			@Override public String getArea(TB_Address it) throws Exception {
				TB_City city = town.getCity();
				TB_Province province = city.getProvince();
				String area = province.getName() + "/" + city.getName() + "/" + town.getName();
				return area;
			}
		};
		if(defaultFlag){
			setDefault(bean);
		}
		return bean;
	}
	
	/**
	 * 判断是否存在默认地址
	 */
	@Override
	public Boolean existsDefult(TB_User user){
		return exists(it->it.user.eq(user).and(it.defaultFlag.eq(true)));
	}
	@Override
	public Boolean existsDefult(String skey){
		return exists(it->it.user.skey.eq(skey).and(it.defaultFlag.eq(true)));
	}
	
	/**
	 * 得到默认地址
	 */
	@Override
	public TB_Address getDefult(TB_User user){
		if(existsDefult(user)){
			return findOne(it->it.user.eq(user).and(it.defaultFlag.eq(true)));
		}else{
			return null;
		}
	}
	
	@Override
	public TB_Address getDefult(String skey){
		if(existsDefult(skey)){
			return findOne(it->it.user.skey.eq(skey).and(it.defaultFlag.eq(true)));
		}else{
			return null;
		}
	}
	
	/**
	 * 设置默认地址
	 */
	@Override
	public void setDefault(TB_Address address){
		address = load(address);
		List<TB_Address> addresses = address.getUser().getAddresss();
		for(TB_Address item : addresses){
			item.setDefaultFlag(false);
		}
		address.setDefaultFlag(true);
	}

	@Override
	public Boolean exists(String skey) {
		return exists(it->it.user.skey.eq(skey));
	}
	
}
